package com.settlement.system.service.impl;

import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblWpAuditStaticDtlMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblWpAuditStaticDtl;
import com.settlement.system.service.ITblWpAuditArchiveResultService;
import com.settlement.system.service.ITblWpAuditStaticDtlService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * wp-稽核明细 服务实现类
 * @author xiaochuan
 * @since 2020-11-25
 */
@Service
@Slf4j
public class TblWpAuditStaticDtlServiceImpl extends BaseServiceImpl implements ITblWpAuditStaticDtlService {

    @Resource
    private TblWpAuditStaticDtlMapper tblWpAuditStaticDtlMapper;

    @Resource
    private ITblWpAuditArchiveResultService tblWpAuditArchiveResultService;

    @Override
    public BaseMapper init() {
        return tblWpAuditStaticDtlMapper;
    }

    @Override
    public void deleteByTransactionalNo(long batchNumber) throws ServiceException {
        try {
            tblWpAuditStaticDtlMapper.deleteByTransactionalNo(batchNumber);
        } catch (Exception e) {
           throw new ServiceException(e);
        }

    }

    @Override
    public TblWpAuditStaticDtl getFoot(Map<String, Object> params) throws ServiceException {
        try {
            return tblWpAuditStaticDtlMapper.getFoot(params);
        } catch (Exception ex){
            throw new ServiceException("获取稽核明细合计异常",ex);
        }
    }

    @Override
    public <ModelType> void save(List<ModelType> list) throws ServiceException {
        try {
            tblWpAuditStaticDtlMapper.batchAdd((List<TblWpAuditStaticDtl>) list);
        } catch (Exception ex) {
            log.error("batch add wp audit reports exception:",ex);
            throw new ServiceException("batch add wp audit reports exception:",ex);
        }
    }

    @Override
    public void updateArchiveResult(Map<String, Object> params) throws ServiceException {
        try {
            tblWpAuditStaticDtlMapper.updateArchiveResult(params);
        } catch (Exception e) {
            throw new ServiceException(e);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveArchiveResult(Map<String, Object> params, String shopNos) throws ServiceException {
        if(StringUtils.isNotEmpty(shopNos)){
            List<String> list = Arrays.asList(shopNos.split("/"));
            tblWpAuditArchiveResultService.deleteByShopNoList(list);
            tblWpAuditStaticDtlMapper.updateArchiveResult(params);
        }
    }

}

